home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1209 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.8 KB  |  132 lines

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Newbie question...
  5. Date: 09 Jan 1996 21:35:35 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan9163535@g7240065.bridge.bst.bls.com>
  8. References: <30EDB825.70A8@rgs.navsea.navy.mil>
  9. NNTP-Posting-Host: bstfirewall.bst.bls.com
  10.  
  11.  
  12. : Dan Loomis wrote:
  13. :> 
  14. :> Is there any way (or to put it a better way, acceptable), to return a
  15. :> member function reference in order to access private class data?
  16. :> 
  17. :> //For example...
  18. :> 
  19. :> class clsMyClass
  20. :> {
  21. :> private:
  22. :>         int iResult;
  23. :> public:
  24. :>         [return type] Result([Arg1]);
  25. :> };
  26. :> 
  27. :> //the usual function declarations...
  28. :> 
  29. :> //...so that I can do this?
  30. :> 
  31. :> main()
  32. :> {
  33. :>         clsMyClass objMyClass;
  34. :>         int iBuffer;
  35. :> 
  36. :>         iBuffer = objMyClass.Result();
  37. :> 
  38. :>         //and do this...
  39. :> 
  40. :>         objMyClass.Result() = 3;
  41. :> 
  42. :>         //Any guesses where I got this awful idea from?
  43. :> };
  44.  
  45. Yes
  46.  
  47.   class clsMyClass
  48.   { 
  49.     public:
  50.       int& Result(void)
  51.       { return iResult; }
  52.  
  53.     private:
  54.       int iResult;
  55.   };
  56.  
  57. This would allow you to do what you want.
  58.  
  59. :> Is this acceptable programming practice, or should I just define
  60. :> something like getResult() and setResult(int)?
  61.  
  62. The reason there would be problems with this is, if at a later stage you
  63. decide you don't want to keep the state of that variable but calculate
  64. it then all code that uses these functions breaks.
  65.  
  66. What you can do is encapsulate this problem in a class say ResultProperty
  67. [ *Warning untested code - writing straight into editor ;']
  68.  
  69.   class ResultProperty
  70.   {
  71.     public:
  72.       const Property& operator = (int result)
  73.       { result_ = result; return *this; }
  74.       operator int (void)
  75.       { return result_; }
  76.  
  77.     private:
  78.       int result_;
  79.   };
  80.  
  81. now the clsMyClass becomes
  82.  
  83.   class clsMyClass
  84.   { 
  85.     public:
  86.       ResultProperty& Result(void)
  87.       { return result_; }
  88.  
  89.     private:
  90.       ResultProperty result_;
  91.   };
  92.  
  93. Its use would be as your main i.e: a.Result() = 5;
  94. At a later stage if you wish to calculate things or get the information
  95. from somewhere else you can change the ResultProperty class and client
  96. code will not break.
  97.  
  98. I am not saying I advocate this solution, but it is better than returning
  99. a direct reference to state.
  100.  
  101. Normally I would use function overloading to distinguish between accessor and
  102. mutator like:
  103.  
  104.   class clsMyClass
  105.   { 
  106.     public:
  107.       int result(void)
  108.       { return result_; }
  109.  
  110.       void result(int r)
  111.       { result_ = r; }
  112. or
  113.       int result(int r)
  114.       { int tmp = result_; result_ = r; return tmp; }
  115.  
  116.     private:
  117.       int result_;
  118.   };
  119.  
  120. And use it like
  121.  
  122.   int old = cls.result(new);
  123.   ...
  124.   cls.result(old);
  125.  
  126. Hope this helps
  127. Regards
  128.  
  129.    -A.
  130. -- 
  131. | A.Champion                |
  132.